package com.wangrui.client;


import gwtupload.client.MultiUploader;
import gwtupload.client.Uploader;

import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Side;
import com.smartgwt.client.util.KeyCallback;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.grid.CellFormatter;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.events.DataArrivedEvent;
import com.smartgwt.client.widgets.tree.events.DataArrivedHandler;
import com.wangrui.client.data.FileNameXmlDS;
import com.wangrui.client.data.SipfA01XmlDS;
import com.wangrui.client.data.SipfA02XmlDS;
import com.wangrui.client.data.WorldXmlDS;
import com.wangrui.client.form.SipfLogForm;
import com.wangrui.client.form.Up;
import com.wangrui.client.grid.SipfLogGrid;
import com.wangrui.client.grid.Sipfa01Grid;
import com.wangrui.client.grid.Sipfa04Grid;
import com.wangrui.client.window.SipfLogWin;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;



/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Logsys implements EntryPoint, AsyncCallback {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);

	/**
	 * This is the entry point method.
	 */

	public String bodyStyle = "text-align:center;padding:0 0;"
//      + "border:1px dotted #99bbe8;background:#dfe8f6;"
      + "color:#15428b;"
      + "font:bold 48px tahoma,arial,sans-serif;"
      ;
	
	private VLayout mainLayout;
	private HLayout twoPanel;
	private Window loginWin;
	private Label welcomeLabe;

	public void onModuleLoad() {
		
		Img logImg = new Img("wangrui.jpg",550,400);
		logImg.setAppImgDir("backgrounds/");
		
		loginWin = new Window();
		loginWin.setTitle("Login");
		loginWin.setAlign(Alignment.CENTER);
		loginWin.setWidth(570);
		loginWin.setHeight(480);
		loginWin.setCanDrag(false);
		loginWin.setShowMinimizeButton(false);
		loginWin.setIsModal(true);
		loginWin.setShowModalMask(true);
		loginWin.centerInPage();
		loginWin.setShowCloseButton(false);
		//loginWin.addItem(logImg);
		loginWin.show();

		final DynamicForm logTimeForm = new DynamicForm();
		logTimeForm.setNumCols(4);
		logTimeForm.setLayoutAlign(Alignment.CENTER);
		logTimeForm.setTitle("login");
		final TextItem username = new TextItem("CODE", "username");
		username.setRequired(true);
		final PasswordItem password = new PasswordItem("PSW", "password");
		
		final ButtonItem login = new ButtonItem("login");
		logTimeForm.setItems(username, password, login);

		loginWin.addItem(logTimeForm);

		login.addClickHandler(new ClickHandler() {
			//
			
			public void onClick(ClickEvent event) {

			
				String id = (String) username.getValue();
				String psw = (String) password.getValue();
				
				if(id==null){
					SC.say("请输入用户名");
				}

				GreetingServiceAsync loginService = GreetingService.Util
						.getInstance();

				AsyncCallback cb_login = new AsyncCallback() {

					@Override
					public void onFailure(Throwable caught) {}

					public void onSuccess(Object result) {
												System.out.println("登陆情况！！！"+result.toString() );

						Boolean ok = (Boolean) result;

						if (ok.booleanValue()) {

							loginWin.clear();

							SC.say("login sucess!");

							mainLayout = new VLayout();
							mainLayout.setWidth100();
							mainLayout.setHeight100();
							mainLayout.setMembersMargin(2);

							CreatNorthPanel();
							CreateSecPanel();
							CreateSouthPanel();

							mainLayout.draw();
						} else
							SC.say("用户名或密码不正确，请重新你输入！");
						    username.setValue("");
						    password.setValue("");
					}
				};
				loginService.login(id, psw, cb_login);

			}

		});

	}

	private void CreatNorthPanel() {
		
		welcomeLabe = new Label("Login Sucess!");
		welcomeLabe.setBaseStyle(bodyStyle);
		welcomeLabe.setID("welcome");
		
		
		Img titleimg =  new Img("title.jpg",800,55);
		titleimg.setAppImgDir("backgrounds/");
		VLayout northPanel = new VLayout();
		northPanel.setShowEdges(true);
		northPanel.setEdgeImage("edges/blue/9_R.png");
		//northPanel.setPadding(10);
		northPanel.setHeight(56);
		northPanel.setMembersMargin(2);
		northPanel.setLayoutMargin(2);
		northPanel.setShowResizeBar(true);
		northPanel.setBackgroundColor("#C3D9FF");
		northPanel.addChild(titleimg);
	//	northPanel.addChild(welcomeLabe);
		

		ToolStrip topBar = new ToolStrip();
		topBar.setHeight(50);
		topBar.setWidth100();
		topBar.setBackgroundColor("#C3D9FF");

		Label title = new Label("系统名称");

		title.setAlign(Alignment.CENTER);
		title.setWidth100();
		topBar.addMember(title);
		
		

		// northPanel.addMember(topBar);

		mainLayout.addMember(northPanel);
	}

	private void CreateSouthPanel() {
		VLayout southPanel = new VLayout();
		southPanel.setShowEdges(true);
		southPanel.setEdgeImage("edges/blue/9_R.png");
		southPanel.setPadding(10);
		southPanel.setHeight(56);
		southPanel.setMembersMargin(2);
		southPanel.setLayoutMargin(2);
		southPanel.setShowResizeBar(true);
		southPanel.setBackgroundColor("#C3D9FF");
		mainLayout.addMember(southPanel);
	}

	private void CreateSecPanel() {

		twoPanel = new HLayout();
		// twoPanel.setShowEdges(true);
		twoPanel.setPadding(10);
		twoPanel.setMembersMargin(2);
		twoPanel.setLayoutMargin(2);
		twoPanel.setShowResizeBar(true);

		HLayout westPanel = new HLayout();
		westPanel.setWidth(300);
		westPanel.setMembersMargin(2);
		westPanel.setLayoutMargin(5);

		final HLayout centerPanel = new HLayout();
		centerPanel.setMembersMargin(2);
		centerPanel.setLayoutMargin(2);

		final TabSet tabSet = new TabSet();
		tabSet.setTabBarPosition(Side.TOP);
		tabSet.setWidth100();
		tabSet.setHeight100();

		// tab1
		Tab tTab1 = new Tab("test", "");
//		final DynamicForm form = new DynamicForm();
//		final TextItem usernametxt = new TextItem("username");
//		usernametxt.setTitle("NAME");
//		final PasswordItem passwdtxt = new PasswordItem("password");
//		passwdtxt.setTitle("PASSWORD");
//		ButtonItem login = new ButtonItem("login");
//		form.setFields(usernametxt, passwdtxt, login);
		
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		
		
//		ListGrid sipfa01 = new ListGrid(); 
//		sipfa01.setWidth100();   
//	    sipfa01.setHeight100();
//	    sipfa01.setDataSource(SipfA01XmlDS.getInstance());  
//		
//	    DataSource sipfa01DS = DataSource.get("sipfa01");
//		sipfa01.setDataSource(sipfa01DS);
//		sipfa01.setUseAllDataSourceFields(true);
//		sipfa01.setShowFilterEditor(true);
//		sipfa01.setAlternateRecordStyles(true);   
//        sipfa01.setAutoFetchData(true);   
//        sipfa01.setShowFilterEditor(true); 
//		sipfa01.show();
//        ListGridField OC_DATE = new ListGridField("OC_DATE", "日期");   
//        ListGridField ZQGSDM = new ListGridField("ZQGSDM", "证券公司代码");   
//        ListGridField ZQGSMC = new ListGridField("ZQGSMC", "证券公司名称");
//        ListGridField JSCYRDM = new ListGridField("JSCYRDM", "结算参与人代码");   
//        ListGridField JJXSRDM = new ListGridField("JJXSRDM", "基金销售人代码");   
//        ListGridField JSCYRQSBH = new ListGridField("JSCYRQSBH", "结算参与人清算编号");
//        ListGridField JSCYRJSZH = new ListGridField("JSCYRJSZH", "计算参与人结算账户");   
//        ListGridField SJRQ = new ListGridField("SJRQ", "数据日期");   
//        
//        sipfa01.setFields(OC_DATE, ZQGSDM, ZQGSMC,JSCYRDM,JJXSRDM,JSCYRQSBH,JSCYRJSZH,SJRQ);  
     
        
		
		    final ListGrid countryGrid = new ListGrid();   
	        countryGrid.setWidth(500);   
	        countryGrid.setHeight(300);   
	        countryGrid.setDataSource(WorldXmlDS.getInstance());   
	  
	        ListGridField countryCodeField = new ListGridField("countryCode", "王锐", 50);   
	        ListGridField nameField = new ListGridField("countryName", "Country");   
	        ListGridField capitalField = new ListGridField("capital", "Capital");   
	        ListGridField continentField = new ListGridField("continent", "Continent");   
	  
	        countryGrid.setFields(countryCodeField, nameField, capitalField, continentField);   
	  
	        countryGrid.setAutoFetchData(true);   
	        countryGrid.setShowFilterEditor(true); 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~		
		
//		sipfa01TestList.setDataSource(sipfA01);   
//        sipfa01TestList.setAutoFetchData(true); 
//        sipfa01TestList.setAutoFitData(Autofit.VERTICAL);   
//        sipfa01TestList.setShowFilterEditor(true);   
//        sipfa01TestList.setAutoFitMaxRecords(10); 
        
//        final DynamicForm exportForm = new DynamicForm();   
//        exportForm.setWidth(300);   
//  
//        SelectItem exportTypeItem = new SelectItem("exportType", "Export Type");   
//        exportTypeItem.setWidth("*");   
//        exportTypeItem.setDefaultToFirstOption(true);   
//  
//        LinkedHashMap valueMap = new LinkedHashMap();   
//        valueMap.put("csv", "CSV (Excel)");   
//        valueMap.put("xml", "XML");   
//        valueMap.put("json", "JSON");   
//        valueMap.put("xls", "XLS (Excel97)");   
//        valueMap.put("ooxml", "XLSX (Excel2007/OOXML)");
//        
//        exportTypeItem.setValueMap(valueMap);   
//        
//        BooleanItem showInWindowItem = new BooleanItem();   
//        showInWindowItem.setName("showInWindow");   
//        showInWindowItem.setTitle("Show in Window");   
//        showInWindowItem.setAlign(Alignment.LEFT);   
        
//        IButton exportButton = new IButton("Export");   
//        
//        exportButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
//			
//   			@Override
//   			public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {
//   				
//   				String exportAs = (String) exportForm.getField("exportType").getValue();   
//   			  
//                FormItem item = exportForm.getField("showInWindow");   
//                boolean showInWindow =  item.getValue() == null ? false : (Boolean) item.getValue();   
//  
//                if(exportAs.equals("json")) {   
//                    // JSON exports are server-side only, so use the OperationBinding on the DataSource   
//                    DSRequest dsRequestProperties = new DSRequest();   
//                    dsRequestProperties.setOperationId("customJSONExport");   
//                    dsRequestProperties.setExportDisplay(showInWindow ? ExportDisplay.WINDOW : ExportDisplay.DOWNLOAD);   
//  
//                    sipfa01TestList.exportData(dsRequestProperties);   
//                } else {   
//                   // exportAs is either XML or CSV, which we can do with requestProperties   
//                    DSRequest dsRequestProperties = new DSRequest();   
//                    dsRequestProperties.setExportAs((ExportFormat)EnumUtil.getEnum(ExportFormat.values(), exportAs));   
//                    dsRequestProperties.setExportDisplay(showInWindow ? ExportDisplay.WINDOW : ExportDisplay.DOWNLOAD);   
//  
//                    sipfa01TestList.exportData(dsRequestProperties);   
//                }   
//   				
//   				
//   			}
//   		});
  
//        exportForm.setItems(exportTypeItem, showInWindowItem); 
//        
//        VLayout layout = new VLayout(15);   
//        layout.setAutoHeight();   
//  
//        HLayout formLayout = new HLayout(15);   
//        formLayout.addMember(exportForm);   
//        formLayout.addMember(exportButton);   
//        layout.addMember(formLayout);  
//	----------------------------------------------------------------
		ListGrid sipfa02 = new ListGrid(); 
		sipfa02.setWidth100();   
		sipfa02.setHeight100();
		sipfa02.setDataSource(SipfA02XmlDS.getInstance());  
		sipfa02.setAutoFetchData(true); 
		sipfa02.show();
        ListGridField OC_DATE = new ListGridField("OC_DATE", "日期");   
        ListGridField ZQGSDM = new ListGridField("ZQGSDM", "证券公司代码");   
        ListGridField YYBDM = new ListGridField("YYBDM", "营业部代码");
        ListGridField YYBMC = new ListGridField("YYBMC", "营业部名称");   
        ListGridField YYBNBDM = new ListGridField("YYBNBDM", "营业部内部代码");   
        ListGridField SJRQ = new ListGridField("SJRQ", "数据日期");     
        sipfa02.setFields(OC_DATE, ZQGSDM, YYBDM,YYBMC,YYBNBDM,SJRQ);  
        
          
		sipfa02.setShowFilterEditor(true);
     
//  ----------------------------------------------------------------
		
		
	
		tTab1.setPane(sipfa02);
		tabSet.addTab(tTab1);
		centerPanel.addChild(tabSet);

		DataSource fileNameXmlDS = FileNameXmlDS.getInstance();
		final TreeGrid treeGrid = new TreeGrid();
		treeGrid.setLoadDataOnDemand(true);
		treeGrid.setWidth(300);
		treeGrid.setHeight100();
		treeGrid.setDataSource(fileNameXmlDS);
		treeGrid.setNodeIcon("icons/24/text_align_center.png");
		treeGrid.setFolderIcon("icons/24/text_align_center.png");
		treeGrid.setShowOpenIcons(false);
		treeGrid.setShowDropIcons(false);
		treeGrid.setClosedIconSuffix("");
		treeGrid.setAutoFetchData(true);
		
		final TreeGridField field = new TreeGridField();
		field.setName("文件名称");
		field.setCellFormatter(new CellFormatter() {
			public String format(Object value, ListGridRecord record,
					int rowNum, int colNum) {
				return record.getAttribute("Name");

			}
		});
		treeGrid.setFields(field);

		treeGrid.addDataArrivedHandler(new DataArrivedHandler() {
			public void onDataArrived(DataArrivedEvent event) {
				treeGrid.getData().openAll();
			}
		});
		
		treeGrid.addCellClickHandler(new CellClickHandler() {

			@Override
			public void onCellClick(CellClickEvent event) {
				// TODO Auto-generated method stub
//				SC.say(event.getRecord().getAttribute("Phone"));
//
				Tab tTab2 = new Tab(event.getRecord().getAttribute("Phone"), "");
////				tabSet.addTab(tTab2);
////				tabSet.selectTab(tTab2);
////				centerPanel.addChild(tabSet);
//				DataSource sipfDS = DataSource.get("sipfa01");
//				
//				ListGrid sipf_grid = new ListGrid();
//				sipf_grid.setWidth100();
//				sipf_grid.setHeight100();
//				sipf_grid.setAutoFetchData(true);
//				sipf_grid.setDataSource(sipfDS);
//				sipf_grid.setUseAllDataSourceFields(true);
//				sipf_grid.show();
//
				tTab2.setIcon("icons/24/text_align_center.png");
				tTab2.setCanClose(true);
				
//				Sipfa01Grid sipfa01Grid = new Sipfa01Grid();
//				sipfa01Grid.show();
//				tTab2.setPane(sipfa01Grid);
//
//			    tabSet.addTab(tTab2);
//				tabSet.selectTab(tTab2);
//				centerPanel.addChild(tabSet);

				if (event.getRecord().getAttribute("Job").equals("sipflog")) {

//				
//					final ListGrid sipflog = new ListGrid();
//					sipflog.setWidth100();
//					sipflog.setHeight100();
//					sipflog.setAutoFetchData(true);
//					sipflog.setDataSource(sipflogDS);// 璁剧疆DataSource
//					sipflog.setUseAllDataSourceFields(true);
//					sipflog.addCellClickHandler(new CellClickHandler() {
//
//						@Override
//						public void onCellClick(CellClickEvent event) {
//							// TODO Auto-generated method stub
//							// SC.say("click it ");
//							logTimeForm.setDataSource(sipflogDS);
//							ListGridRecord record = sipflog.getSelectedRecord();
//							logTimeForm.editRecord(record);
//
//							Window sipflogWin = new Window();
//							sipflogWin.setTitle("log");
//							sipflogWin.setAlign(Alignment.CENTER);
//							sipflogWin.setWidth(300);
//							sipflogWin.setHeight(150);
//							sipflogWin.setShowMinimizeButton(false);
//							sipflogWin.setIsModal(true);
//							sipflogWin.setShowModalMask(true);
//							sipflogWin.centerInPage();
//
//							sipflogWin.addItem(logTimeForm);
//							sipflogWin.show();
//						}
//					});

//					sipflog.show();
//					tTab2.setIcon("icons/24/text_align_center.png");
//					tTab2.setPane(sipflog);
			
					
					final SipfLogGrid sipfLogGrid = new SipfLogGrid();
					sipfLogGrid.show();
					
					
					
//					sipfLogGrid.addCellClickHandler(new CellClickHandler()
//					{
//						
//						public void onCellClick(CellClickEvent event) {
//
//							SipfLogWin  sipfLogWin = new SipfLogWin();
//							
//							ListGridRecord record = sipfLogGrid.getSelectedRecord();
//							//sipfLogWin.setTitle(record.getAttribute("OC_DATE"));
//							sipfLogWin.show();
//							
//						}
//					});
					
					
					tTab2.setPane(sipfLogGrid);
					tabSet.addTab(tTab2);
					tabSet.selectTab(tTab2);
					centerPanel.addChild(tabSet);

				} else if(event.getRecord().getAttribute("Job").equals("zdsjbxlog")){
					
					//SC.say("这是中登数据报送日志！");
					Up multi = new Up();
					multi.show();
				} else if(event.getRecord().getAttribute("Job").equals("fxqlog")){
					
					SC.say("这是反洗钱监控系统日志！");
				} else if(event.getRecord().getAttribute("Job").equals("createSipfLog")){
					
					SipfLogForm  sipflogform = new SipfLogForm();
					sipflogform.show();
		
				} else if (event.getRecord().getAttribute("Job").equals("A01")){
					
					Sipfa01Grid sipfa01Grid = new Sipfa01Grid();
					sipfa01Grid.show();
					tTab2.setPane(sipfa01Grid);
	
				    tabSet.addTab(tTab2);
					tabSet.selectTab(tTab2);
					centerPanel.addChild(tabSet);
				} else if (event.getRecord().getAttribute("Job").equals("A04")){
					
					Sipfa04Grid sipfa04Grid = new Sipfa04Grid();
					sipfa04Grid.show();
					tTab2.setPane(sipfa04Grid);
	
				    tabSet.addTab(tTab2);
					tabSet.selectTab(tTab2);
					centerPanel.addChild(tabSet);
				}
				   

			}
		});
		treeGrid.draw();

		twoPanel.addMember(treeGrid);
		twoPanel.addMember(centerPanel);
		mainLayout.addMember(twoPanel);
		
		 if (!GWT.isScript()) { 	
			    KeyIdentifier debugKey = new KeyIdentifier(); 
			    debugKey.setCtrlKey(true); 
			    debugKey.setKeyName("A"); 
			    Page.registerKey(debugKey, new KeyCallback() { 
			        public void execute(String keyName) { 
				    SC.showConsole(); 
				 } 
			    }); 
		 }
		
		
	}

	@Override
	public void onFailure(Throwable caught) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSuccess(Object result) {
		// TODO Auto-generated method stub

	}

}
